﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace KD.Sms.Monitor
{
    public partial class frmDepartTwo : Form
    {
        private Hashtable _HasControl = null;
        public frmDepartTwo()
        {
            InitializeComponent();
        }

        private void frmDepartN_Load(object sender, EventArgs e)
        {
            ActionCenter.Action.Instance.SamplingPosMaking += OnSamplingPosMaking;
            _HasControl = new Hashtable();
            foreach (Control cl in panel11.Controls)
            {
                if (cl.GetType().Name.ToLower() == "panel")
                {
                    if (_HasControl[cl.Name.ToUpper()] == null)
                        _HasControl.Add(cl.Name.ToUpper(), cl);
                }
            }

            if (ActionCenter.Action.Instance.E_SamplingPos != null && ActionCenter.Action.Instance.E_SamplingPos.Count > 0)
            {
                DoShowPostion(ActionCenter.Action.Instance.E_SamplingPos);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="pane"></param>
        /// <param name="e"></param>
        private void DoPaint(Panel pane, PaintEventArgs e)
        {
            ControlPaint.DrawBorder(e.Graphics,
                 pane.ClientRectangle,
                 Color.Lime,
                 1,
                 ButtonBorderStyle.Solid,
                 Color.Lime,
                 1,
                 ButtonBorderStyle.Solid,
                 Color.Lime,
                 1,
                 ButtonBorderStyle.Solid,
                 Color.Lime,
                 1,
                 ButtonBorderStyle.Solid);
        }

        /// <summary>
        /// 设置窗体初始值
        /// </summary>
        /// <param name="pos"></param>
        public void DoSetPart()
        {
            foreach (Control cl in this.Controls)
            {
                if (cl.GetType().Name.ToLower() == "panel")
                {
                    cl.BackColor = Color.Transparent;
                }
            }
        }

        /// <summary>
        /// 设置窗体初始值
        /// </summary>
        /// <param name="pos"></param>
        public void OnSamplingPosMaking(object send, ActionCenter.CommonEventArgs e)
        {
            DoShowPostion((List<Entity.SamplingPos>)e.E_Values);
        }

        private void DoShowPostion(List<Entity.SamplingPos> pos)
        {
            foreach (Control cl in panel11.Controls)
            {
                if (cl.GetType().Name.ToLower() == "panel")
                {
                    UI.FormHelper.SetBackColor(cl, Color.Transparent);
                }
            }

            //设置默认值
            if (pos != null && _HasControl != null)
            {
                Panel p;
                foreach (Entity.SamplingPos posChild in pos)
                {
                    if (_HasControl["P" + posChild.BlockNumber.ToString()] != null)
                    {
                        p = (Panel)_HasControl["P" + posChild.BlockNumber.ToString()];
                        if (p != null)
                        {
                            UI.FormHelper.SetBackColor(p, Color.Green);
                        }
                    }
                }

                //foreach (Entity.SamplingPos posChild in pos)
                //{
                //    string[] _arr = new string[] { P16.ToString(), P17.ToString(), P18.ToString()};//组数1
                //    string[] _arr1 = new string[] {P13.ToString(),P14.ToString(),P15.ToString()};//组数2
                //    string[] _arr2 = new string[] {P10.ToString(),P11.ToString(),P12.ToString()};//组数3
                //    string[] _arr3 = new string[] {P7.ToString(),P8.ToString(),P9.ToString()};//组数4
                //    string[] _arr4 = new string[] {P4.ToString(),P5.ToString(),P6.ToString()};//组数5
                //    string[] _arr5 = new string[] {P1.ToString(),P2.ToString(),P3.ToString()};//组数6

                //    if (_HasControl["P" + posChild.BlockNumber.ToString()] != null)
                //    {
                //        int i = Convert.ToInt32(_arr.Length)-15;
                //        p = (Panel)_HasControl["P" + i.ToString()];
                //        if (p != null)
                //        {
                //            UI.FormHelper.SetBackColor(p, Color.Green);
                //        }
                //        int i1 = Convert.ToInt32(_arr1.Length)-9;
                //        p = (Panel)_HasControl["P" + i1.ToString()];
                //        if (p != null)
                //        {
                //            UI.FormHelper.SetBackColor(p, Color.Green);
                //        }
                //        int i2 = Convert.ToInt32(_arr2.Length)-3;
                //        p = (Panel)_HasControl["P" + i2.ToString()];
                //        if (p != null)
                //        {
                //            UI.FormHelper.SetBackColor(p, Color.Green);
                //        }
                //        int i3 = Convert.ToInt32(_arr3.Length)+3;
                //        p = (Panel)_HasControl["P" + i3.ToString()];
                //        if (p != null)
                //        {
                //            UI.FormHelper.SetBackColor(p, Color.Green);
                //        }
                //        int i4 = Convert.ToInt32(_arr4.Length)+9;
                //        p = (Panel)_HasControl["P" + i4.ToString()];
                //        if (p != null)
                //        {
                //            UI.FormHelper.SetBackColor(p, Color.Green);
                //        }
                //        int i5 = Convert.ToInt32(_arr5.Length)+15;
                //        p = (Panel)_HasControl["P" + i5.ToString()];
                //        if (p != null)
                //        {
                //            UI.FormHelper.SetBackColor(p, Color.Green);
                //        }
                //    }
                //}
            }

        }

        /// <summary>
        /// 设置网格显示
        /// </summary>
        /// <param name="pos"></param>
        public bool DoSetPartState(int pos)
        {
            Panel p;
            bool _bPart = false;
            if (_HasControl != null && _HasControl.Count > 0 && _HasControl["panel" + pos.ToString()] != null)
            {
                p = (Panel)_HasControl["P" + pos.ToString()];
                if (p != null)
                {
                    UI.FormHelper.SetBackColor(p, Color.Green);
                    _bPart = true;
                }
            }
            return _bPart;
        }

        /// <summary>
        /// 截图获取Image
        /// </summary>
        /// <returns></returns>
        public Image DoGetPartImage()
        {
            Image memory = new Bitmap(295, 135);
            Rectangle rect = new Rectangle();
            rect.Width = 295;
            rect.Height = 135;
            rect.X = this.Location.X + 8;
            rect.Y = this.Location.Y + 49;
            Graphics g = Graphics.FromImage(memory);
            g.CopyFromScreen(rect.X + 1, rect.Y + 1, 0, 0, rect.Size);
            return memory;
        }

        /// <summary>
        /// 截图获取MemoryStream
        /// </summary>
        /// <returns></returns>
        public System.IO.MemoryStream DoGetPartMemoryStream()
        {
            Image memory = new Bitmap(295, 135);
            Rectangle rect = new Rectangle();
            rect.Width = 295;
            rect.Height = 135;
            rect.X = this.Location.X + 8;
            rect.Y = this.Location.Y + 49;
            Graphics g = Graphics.FromImage(memory);
            g.CopyFromScreen(rect.X + 1, rect.Y + 1, 0, 0, rect.Size);

            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            memory.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
            return ms;
        }

        private void P1_Paint(object sender, PaintEventArgs e)
        {
            DoPaint(P1, e);
        }

        private void P2_Paint(object sender, PaintEventArgs e)
        {
            DoPaint(P2, e);
        }

        private void P3_Paint(object sender, PaintEventArgs e)
        {
            DoPaint(P3, e);
        }

        private void P6_Paint(object sender, PaintEventArgs e)
        {
            DoPaint(P6, e);
        }

        private void P4_Paint(object sender, PaintEventArgs e)
        {
            DoPaint(P4, e);
        }

        private void P5_Paint(object sender, PaintEventArgs e)
        {
            DoPaint(P5, e);
        }

        private void P7_Paint(object sender, PaintEventArgs e)
        {
            DoPaint(P7, e);
        }

        private void P8_Paint(object sender, PaintEventArgs e)
        {
            DoPaint(P8, e);
        }

        private void P9_Paint(object sender, PaintEventArgs e)
        {
            DoPaint(P9, e);
        }

        private void P10_Paint(object sender, PaintEventArgs e)
        {
            DoPaint(P10, e);
        }

        private void P11_Paint(object sender, PaintEventArgs e)
        {
            DoPaint(P11, e);
        }

        private void P12_Paint(object sender, PaintEventArgs e)
        {
            DoPaint(P12, e);
        }

        private void P13_Paint(object sender, PaintEventArgs e)
        {
            DoPaint(P13, e);
        }

        private void P14_Paint(object sender, PaintEventArgs e)
        {
            DoPaint(P14, e);
        }

        private void P15_Paint(object sender, PaintEventArgs e)
        {
            DoPaint(P15, e);
        }

        private void P16_Paint(object sender, PaintEventArgs e)
        {
            DoPaint(P16, e);
        }

        private void P17_Paint(object sender, PaintEventArgs e)
        {
            DoPaint(P17, e);
        }

        private void P18_Paint(object sender, PaintEventArgs e)
        {
            DoPaint(P18, e);
        }
    }
}
